<?php

/*
  Criada pra listar os produtos do array de objetos passado
 */

class Template {

    private $template;
	private $blocks = array();
    private $replaces = array();
    
    /**
     * O template deve ser um arquivo na pasta templates
     * @param string $tplName 
     */
	function Template(){
		if(!is_null($this->template)){
			$this->addReplace('ACAOATUAL',FrontController::acao());
			$this->addReplace('APLICACAOATUAL',FrontController::aplicacao());
			$this->addReplace('URL','http://'.$_SERVER['HTTP_HOST'].'/loja/');
			$this->addReplace('PAGINAATUAL','http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
			$this->addReplace('DIR','/loja/');
		}
	}
    public function setTemplate($tplName,$extensao = '.html'){
        $tpl = file_get_contents(PATH_TEMPLATE.$tplName.$extensao);
        $this->template = $tpl;
    }
	public function loadHtml($tplName,$tag = "CONTEUDO"){
		$tpl = file_get_contents(PATH_TEMPLATE.$tplName);
		$this->template = str_replace("{{$tag}}", $tpl, $this->template);
    }
    
	public function addReplace($tag, $value, $Template = false){
        $this->replaces[$tag] = $value;
		foreach ($this->replaces as $tag => $value)
			$this->template = str_replace("{{$tag}}", $value, $this->template);
		return $this->template;
    }
	public function addLocalReplace($tag, $value, $Template = false){
        $this->replaces[$tag] = $value;
		$htmlTemplate =  $Template;
		foreach ($this->replaces as $tag => $value)
			$htmlTemplate = str_replace("{{$tag}}", $value, $htmlTemplate);
		return $htmlTemplate;
    }
	public function hideBlock($blockName) {
		if($this->template != NULL) {
		   $posIni = strpos($this->template, "<!-- BEGIN {$blockName} -->");
		   $posEnd = strpos($this->template, "<!-- END {$blockName} -->");
		   $block = substr($this->template, $posIni, $posEnd-$posIni);
		   $this->template = str_replace($block, '', $this->template);
		}
		return $this->template;
	}
	
	public function setBlock($blockName,$blockVariables = false){
		if($this->template != NULL){
			$ini = "<!-- BEGIN {$blockName} -->";
			$end = "<!-- END {$blockName} -->";
			$posIni = strpos($this->template, $ini);
			$posEnd = strpos($this->template, $end);
			$currentBlock = substr($this->template, $posIni, $posEnd-$posIni);
				
			if(is_array($blockVariables)){
				foreach($blockVariables as $tag => $value)
					$currentBlock = $this->addLocalReplace($tag,$value,$currentBlock);
			}
			$this->blocks[$blockName] .= str_replace(	array($ini,$end),
														array('',''),
														$currentBlock
										);
		}
	}
	public function setTemplateHtml($blocks) {
		if($this->template != NULL) {
			foreach($this->blocks as $blockName => $html){	
				$ini = "<!-- BEGIN {$blockName} -->";
				$end = "<!-- END {$blockName} -->";
				$posIni = strpos($this->template, $ini);
				$posEnd = strpos($this->template, $end);
				$block = substr($this->template, $posIni, $posEnd-$posIni);
				$this->template = str_replace(	$block,
											$html,
											$this->template
										);
			}
		}
    }
    public function render() {
		$this->Template();
		$this->setTemplateHtml($this->blocks);
        return $this->template;
    }

}